package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.enums.RoomTypeEnum;
import com.ruoyi.system.mapper.RoomMapper;
import com.ruoyi.system.service.IRoomService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客房信息Service业务层处理
 *
 * @author 强盛酒店
 * @date 2023-03-07
 */
@Service
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询客房信息
     *
     * @param id 客房信息主键
     * @return 客房信息
     */
    @Override
    public Room selectRoomById(Long id) {
        return roomMapper.selectRoomById(id);
    }

    /**
     * 查询客房信息列表
     *
     * @param room 客房信息
     * @return 客房信息
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        room.setIsDeleted(NumberUtils.INTEGER_ZERO);
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增客房信息
     *
     * @param room 客房信息
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) throws Exception {
        room.setCreateTime(DateUtils.getNowDate());
        room.setCreateUser(SecurityUtils.getUserId());
        room.setName(RoomTypeEnum.getEnum(room.getType()).getName() +"\t"+room.getRoomNum());
        return roomMapper.insertRoom(room);
    }

    /**
     * 修改客房信息
     *
     * @param room 客房信息
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) throws Exception {
        room.setUpdateTime(DateUtils.getNowDate());
        room.setUpdateUser(SecurityUtils.getUserId());
        room.setName(RoomTypeEnum.getEnum(room.getType()).getName() +"\t"+room.getRoomNum());
        return roomMapper.updateRoom(room);
    }

    /**
     * 批量删除客房信息
     *
     * @param ids 需要删除的客房信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return roomMapper.deleteRoomByIds(ids);
    }

    /**
     * 删除客房信息信息
     *
     * @param id 客房信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomById(Long id) {
        return roomMapper.deleteRoomById(id);
    }

    @Override
    public int deleteRoomByIds2(Long[] ids) {
        for (Long id : ids) {
            Room room = roomMapper.selectRoomById(id);
            room.setIsDeleted(NumberUtils.INTEGER_ONE);
            roomMapper.updateRoom(room);
        }
        return 1;
    }
}
